<template>
    <el-container class="layout">
        <el-aside :width="isCollapse ? '64px' : '200px'">
            <div :class="isCollapse ? 'logo logo-mini' : 'logo'">
                <a href="#">
                    <img src="../assets/logo.png" alt />
                    <h1>微服务平台</h1>
                </a>
            </div>
            <div class="menu-dark">
                <el-menu unique-opened router :default-active="activeMenu" :collapse="isCollapse" :collapse-transition="false">
                    <el-submenu v-for="item in menulist" :key="item.id" :index="item.id + ''">
                        <template slot="title">
                            <i :class="iconsObj[item.id]"></i>
                            <span>{{ item.authName }}</span>
                        </template>
                        <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/' + subItem.path">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{ subItem.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </div>
        </el-aside>
        <el-container>
            <el-header>
                <div class="header">
                    <span class="header-trigger" @click="toggleAside">
                        <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
                    </span>
                    <el-button type="info" @click="logout">退出</el-button>
                </div>
            </el-header>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            menulist: [],
            iconsObj: {
                125: 'icon icon-user',
                103: 'icon icon-auth',
                101: 'icon icon-shop',
                102: 'icon icon-order',
                145: 'icon icon-data'
            },
            isCollapse: false
        }
    },
    created() {
        this.getMenuList()
    },
    computed: {
        // 动态设置激活的导航
        activeMenu() {
            return this.$route.path
        }
    },
    methods: {
        // 退出
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取菜单
        async getMenuList() {
            const { data: res } = await this.$http.get('menus')
            if (res.meta.status !== 200) return this.$message.error(res.meta.message)
            this.menulist = res.data
            // console.log(res)
        },
        // 切换左侧菜单
        toggleAside() {
            this.isCollapse = ! this.isCollapse
        }
    }
}
</script>
<style lang="less">
.menu-dark {
    .el-menu {
        border-right: 0;
        background-color: transparent;
        .el-submenu__title,
        .el-menu-item {
            &:hover {
                background-color: transparent;
            }
            &.is-active {
                color: @white;
                background-color: @bg-color-menu-active;
            }
            .icon {
                color: @white;
                margin-right: 10px;
                font-size: 18px;
            }
        }
        .el-menu-item,
        .el-submenu__title {
            color: @white;
        }
        > .el-submenu {
            > .el-menu {
                background-color: @bg-color-submenu;
                .el-menu-item,
                .el-submenu__title {
                    color: #999;
                    &:hover {
                        color: @white;
                        i {
                            color: @white;
                        }
                    }
                    &.is-active {
                        color: @white;
                        background-color: @bg-color-menu-active;
                    }
                }
            }
        }
    }
}
</style>
<style lang="less" scoped>
.layout {
    height: 100%;
    background-color: @bg-color-main;
}
.el-aside {
    background-color: @bg-color-aside;
    box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
    transition: all 0.2s;
    z-index: 1;
}
.el-header {
    padding-left: 0;
    background-color: @white;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}
.el-main {
    transition: all 0.2s;
}
.logo {
    position: relative;
    padding: 0 24px;
    overflow: hidden;
    background: #001529;
    cursor: pointer;
    transition: all 0.3s;
    &-mini {
        padding: 0 14px;
        h1 {
            opacity: 0;
        }
    }
    > a {
        height: 60px;
        .flexbox();
        .align-items(center);
    }
    h1 {
        display: inline-block;
        margin: 0 0 0 12px;
        color: #fff;
        font-weight: 600;
        font-size: 20px;
        vertical-align: middle;
        animation: fade-in;
        animation-duration: 0.3s;
    }
    img {
        height: 32px;
    }
}
.header {
    height: 60px;
    line-height: 60px;
    .flexbox();
    .align-items(center);
    .justify-content(space-between);
    .header-trigger {
        padding: 0 20px;
        font-size: 24px;
        cursor: pointer;
        transition: all 0.3s, padding 0s;
    }
}
</style>